use std::collections::HashMap;
pub fn median(numbers: &mut Vec<i32>) -> f32 {
    // TODO: Implement logic here to return the median of the list
    numbers.sort();
    let len = numbers.len();
    // idx start at 0
    if len % 2 == 0 {
        (numbers[len / 2 - 1] as f32 + numbers[len / 2] as f32) / 2_f32
    } else {
        numbers[(len - 1) / 2] as f32
    }
}
pub fn mode(numbers: &Vec<i32>) -> Vec<i32> {
    // TODO: Implement logic here to return the mode of the list
    let mut hm: HashMap<i32, i32> = HashMap::new();
    for &n in numbers {
        let t = hm.entry(n).or_insert(0);
        *t += 1;
    }
    let max = hm.clone().into_values().max().unwrap();
    let r: Vec<(i32, i32)> = hm.into_iter().filter(|(_k, v)| *v == max).collect();
    let mut r: Vec<i32> = r.into_iter().map(|(k, _v)| k).collect();
    r.sort();
    r
}

/*
pub fn median(numbers: &mut Vec<i32>) -> f32 {
    // TODO: Implement logic here to return the median of the list
    let len = numbers.len();
    if len == 0 {
        return 0.;
    }
    numbers.sort();
    match len % 2 == 0 {
        false => (numbers[len / 2]) as f32,
        true => (numbers[len / 2] + numbers[len / 2 - 1]) as f32 / 2.,
    }
}
pub fn mode(numbers: &Vec<i32>) -> Vec<i32> {
    if numbers.is_empty() {
        return Vec::new();
    }
   /* println!("Begin Numbers");
    for number in numbers {
        println!("{}", number);
    }
    println!("End Numbers");
    // TODO: Implement logic here to return the mode of the list
    let mut numbersSorted = numbers.clone();
    numbersSorted.sort();*/
    let mut highestFrequency = 0;
    let mut prevNumber = numbers[0];
    let mut currentFrequency = 0;
    let mut modes = vec![];
    for &number in numbers {
        if number != prevNumber {
            if currentFrequency == highestFrequency {
                modes.push(prevNumber);
            }
            else if currentFrequency > highestFrequency {
                highestFrequency = currentFrequency;
                modes.clear();
                modes.push(prevNumber);
            }
            prevNumber = number;
            currentFrequency = 0;
        }
        currentFrequency += 1;
    }
    if let Some(lastMode) = modes.last().copied() {
        if lastMode != prevNumber {
            if currentFrequency == highestFrequency {
                modes.push(prevNumber);
            }
            else if currentFrequency > highestFrequency {
                highestFrequency = currentFrequency;
                modes.clear();
                modes.push(prevNumber);
            }
        }
    }
    modes
}

*/
